package main

import (
	"fmt"
	"net"
)

type Server struct {
	Ip   string
	Port int
}

//创建server接口
func NewServer(ip string, port int) *Server {

	server := &Server{
		Ip:   ip,
		Port: port,
	}
	return server
}

func (s *Server) handle(con net.Conn) {

	fmt.Println("连接成功")

	b := []byte{}
	n, err := con.Read(b)

	if err != nil {
		fmt.Println("con.read err : ", err)
		return
	}

	fmt.Println(n)

}

func (s *Server) Start() {

	listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", s.Ip, s.Port))

	if err != nil {
		fmt.Println("net.listen err:", err)
		return
	}

	for {

		conn, err := listen.Accept()

		if err != nil {
			fmt.Println("listen.accept err :", err)
			continue
		}

		go s.handle(conn)
	}
}
